;; web 开发相关的设置
(require 'init-package)

;; (push '(css . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-css"))
;;       treesit-language-source-alist)
;; (push '(html . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-html"))
;;       treesit-language-source-alist)

(when (maybe-require-package 'web-mode)
  ;; (add-to-list 'auto-mode-alist '("\\.html\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.[agj]sp\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))

  (setq web-mode-engines-alist
        '(("php"    . "\\.phtml\\'")
          ("blade"  . "\\.blade\\."))
        )
  (setq web-mode-markup-indent-offset 2
        web-mode-css-indent-offset 2
        web-mode-code-indent-offset 4
        web-mode-style-padding 1
        web-mode-script-padding 1
        web-mode-block-padding 0
        web-mode-enable-auto-pairing t
        web-mode-enable-css-colorization t)

  (add-hook 'web-mode-hook
            (lambda ()
              (define-key web-mode-map (kbd "C-m") 'web-mode-tag-match)
              ))

  ;; (setq web-mode-extra-snippets
  ;;     '(("erb" . (("toto" . "<% toto | %>\n\n<% end %>")))
  ;;       ("php" . (("dowhile" . "<?php do { ?>\n\n<?php } while (|); ?>")
  ;;                 ("debug" . "<?php error_log(__LINE__); ?>")))
  ;;      ))

  ;; (setq web-mode-extra-auto-pairs
  ;;     '(("erb"  . (("beg" "end")))
  ;;       ("php"  . (("beg" "end")
  ;;                  ("beg" "end")))
  ;;      ))

  (setq web-mode-ac-sources-alist
        '(("php" . (ac-source-yasnippet ac-source-php-auto-yasnippets))
          ("html" . (ac-source-emmet-html-aliases ac-source-emmet-html-snippets))
          ("css" . (ac-source-css-property ac-source-emmet-css-snippets))))
  (add-hook 'web-mode-before-auto-complete-hooks
            '(lambda ()
               (let ((web-mode-cur-language
                      (web-mode-language-at-pos)))
                 (if (string= web-mode-cur-language "php")
                     (yas-activate-extra-mode 'php-mode)
                   (yas-deactivate-extra-mode 'php-mode))
                 (if (string= web-mode-cur-language "css")
                     (setq emmet-use-css-transform t)
                   (setq emmet-use-css-transform nil)))))
  )

;; html config
(add-hook 'sgml-mode-hook
          (lambda ()
            (setq-local tab-width 2
                        sgml-basic-offset 2)
            (define-key sgml-mode-map (kbd "C-.") #'sgml-close-tag)
            (define-key sgml-mode-map "\M-o" nil)
            (define-key html-mode-map "\M-o" nil)
            (define-key sgml-mode-map "\C-o" #'facemenu-keymap)
            ))

;; emmet-mode
(when (maybe-require-package 'emmet-mode)
  (autoload 'emmet-mode "emmet-mode"
        "Minor mode for editing html or css files." t)

  (dolist (hook '(css-mode-hook sgml-mode-hook web-mode-hook))
    (add-hook hook 'emmet-mode))

  (add-hook 'emmet-mode-hook
            (lambda ()
              ;; 将 emmet-expand-line 绑定到 S-<return> 上，默认的 C-<return> 用于插入新行
              (define-key emmet-mode-keymap (kbd "C-<return>") nil)
              (define-key emmet-mode-keymap (kbd "S-<return>") 'emmet-expand-line)
              )))

(when (maybe-require-package 'rainbow-mode)
  ;; 高亮颜色编码
  (dolist (hook '(css-mode-hook sgml-mode-hook))
    (add-hook hook 'rainbow-mode)))

(provide 'init-web)
;
